﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports Microsoft.Samples.WindowsPhoneCloud.Phone.ViewModel
Imports Microsoft.Samples.WindowsPhoneCloud.StorageClient

Namespace Pages

    Partial Public Class QueueDetailsPage
        Inherits PhoneApplicationPage
        Public Sub New()
            Me.InitializeComponent()

            Me.ViewModel = New QueueDetailsPageViewModel()
        End Sub

        Public Property ViewModel() As QueueDetailsPageViewModel
            Get
                Return TryCast(Me.DataContext, QueueDetailsPageViewModel)
            End Get
            Set(ByVal value As QueueDetailsPageViewModel)
                Me.DataContext = value
            End Set
        End Property

        Protected Overrides Sub OnNavigatedTo(ByVal e As NavigationEventArgs)
            Me.ViewModel.Queue = GetApplicationState(Of ICloudQueue)("CurrentQueue")
            If Me.ViewModel.Queue Is Nothing Then
                ' If there is not any queue in the application state, then go back.
                Me.NavigationService.GoBack()
            Else
                RemoveApplicationState("CurrentQueue")
            End If
        End Sub

        Private Shared Function GetApplicationState(Of T)(ByVal key As String) As T
            If Not PhoneApplicationService.Current.State.ContainsKey(key) Then
                Return Nothing
            End If

            Return CType(PhoneApplicationService.Current.State(key), T)
        End Function

        Private Shared Sub RemoveApplicationState(ByVal key As String)
            If PhoneApplicationService.Current.State.ContainsKey(key) Then
                PhoneApplicationService.Current.State.Remove(key)
            End If
        End Sub

        Private Sub OnQueueMessage(ByVal sender As Object, ByVal e As RoutedEventArgs)
            If Me.ViewModel IsNot Nothing Then
                If String.IsNullOrEmpty(Me.ViewModel.QueueMessageContent) Then
                    MessageBox.Show("The queue message cannot be null nor empty.", "Queue Message Result", MessageBoxButton.OK)
                Else
                    Me.ViewModel.QueueMessage()
                End If
            End If
        End Sub

        Private Sub OnDequeueMessage(ByVal sender As Object, ByVal e As RoutedEventArgs)
            If Me.ViewModel IsNot Nothing Then
                Me.ViewModel.DequeueMessage()
            End If
        End Sub

        Private Sub OnDeleteQueueMessages(ByVal sender As Object, ByVal e As EventArgs)
            If Me.ViewModel IsNot Nothing Then
                Me.ViewModel.CloudQueueMessages.Clear()
            End If
        End Sub
    End Class
End Namespace